package com.sne.airways.route.service.impl;

import com.sne.airways.basecore.dao.DBHelper;
import com.sne.airways.basecore.exceptions.AppException;
import com.sne.airways.basecore.service.impl.BaseServiceImpl;
import com.sne.airways.route.pojo.ExpAirPort;
import com.sne.airways.route.pojo.ExpCountry;
import com.sne.airways.route.service.ConPortService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: 卞京帅{bjslogin@gmail.com}
 * Date: 12-11-20
 * Time: 下午11:40
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ConPortServiceImpl extends BaseServiceImpl implements ConPortService {

    @Override
    public List<ExpCountry> getCountry(Integer start, Integer limit, Object... param) throws AppException {
        return findByHQL(ExpCountry.class,start,limit,param);
    }

    @Override
    public Integer getCountryCount(Object... param) throws AppException {
        return findCountByHQL(ExpCountry.class,param);
    }

    @Override
    public List<ExpAirPort> getAirPort(Integer start, Integer limit, Object... param) throws AppException {
        return findByHQL(ExpAirPort.class,start,limit,param);
    }

    @Override
    public Integer getAirPortCount(Object... param) throws AppException {
        return findCountByHQL(ExpAirPort.class,param);
    }

    @Override
    public void saveCountry(String data) throws AppException {
        ExpCountry country = (ExpCountry)JSONObject.toBean(JSONObject.fromObject(data),ExpCountry.class);
        if(StringUtils.isBlank(country.getCountryId())){
            country.setCountryId(null);
            DBHelper.setBaseProperties(country);
            saveObject(country);
        }else{
            ExpCountry exist = getObject(ExpCountry.class,country.getCountryId());
            BeanUtils.copyProperties(country,exist,DBHelper.IGNOREPROPERTIES);
            updateObject(exist);
        }
    }

    @Override
    public void savePort(String data) throws AppException {
        ExpAirPort airPort = (ExpAirPort)JSONObject.toBean(JSONObject.fromObject(data),ExpAirPort.class);
        if(StringUtils.isBlank(airPort.getPortId())){
            DBHelper.setBaseProperties(airPort);
            saveObject(airPort);
        }else{
            ExpAirPort exist = getObject(ExpAirPort.class,airPort.getPortId());
            BeanUtils.copyProperties(airPort,exist,DBHelper.IGNOREPROPERTIES);
            updateObject(exist);
        }
    }


}
